﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GomMinhLongBL;
using GomMinhLongDO;

namespace GomMinhLong.Module
{
    public partial class ucTag : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Page"] != null)
            {
                if (Request.QueryString["Page"].ToString() == "Chi-Tiet-San-Pham")
                {
                    int id = int.Parse(Request.QueryString["ID"].ToString());
                    cmsSanPhamDO objSanPham = new cmsSanPhamDO();
                    objSanPham.SanPhamID = id;
                    objSanPham = new cmsSanPhamBL().Select(objSanPham);
                    if (!string.IsNullOrEmpty(objSanPham.TuKhoa))
                    {
                        TagDataBindding(objSanPham.TuKhoa);
                    }
                }
                if ((Request.QueryString["Page"].ToString() == "Chi-Tiet-Khuyen-Mai") || (Request.QueryString["Page"].ToString() == "Chi-Tiet-Tin-Tuc"))
                {
                    int id = int.Parse(Request.QueryString["ID"].ToString());
                    cmsArticleDO oblArticle = new cmsArticleDO();
                    oblArticle.ArticleID = id;
                    oblArticle = new cmsArticleBL().Select(oblArticle);
                    if (!string.IsNullOrEmpty(oblArticle.Tags))
                    {
                        TagDataBindding(oblArticle.Tags);
                    }
                }
            }
        }
        protected void TagDataBindding(string tag)
        {
            string TotalTag = "";
            TotalTag = tag;
            DataTable dt = new DataTable();
            dt.Columns.Add("TagItem");
            string tagItem = "";
            DataRow row;
            for (int i = 0; i < TotalTag.Length; i++)
            {
                if (TotalTag[i] != ',')
                {
                    tagItem += TotalTag[i];
                }
                else
                {
                    row = dt.NewRow();
                    row[0] = tagItem.Trim();
                    dt.Rows.Add(row);
                    tagItem = "";
                }
                if (tagItem.Length > 0)
                    if (i == TotalTag.Length - 1 && TotalTag[i] != ',')
                    {
                        row = dt.NewRow();
                        row[0] = tagItem.Trim();
                        dt.Rows.Add(row);
                        tagItem = "";
                    }
            }
            rptTag.DataSource = dt;
            rptTag.DataBind();
        }

        public string ChangeAV(string s)
        {
            return Ultility.Change_AV(s);
        }

        protected void rptTag_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex < 0)
            {
                return;
            }
            DataRowView dataItem = (DataRowView)e.Item.DataItem;
            HyperLink hplTag = (HyperLink)e.Item.FindControl("hplTag");

            if (hplTag != null)
            {
                if (Request.QueryString["Page"] != null)
                {
                    if (Request.QueryString["Page"].ToString() == "Chi-Tiet-San-Pham")
                    {
                        hplTag.NavigateUrl = "/Tu-Khoa-San-Pham/" + dataItem["TagItem"].ToString() + "/Tim-Kiem.htm";
                    }
                    else if (Request.QueryString["Page"].ToString() == "Chi-Tiet-Khuyen-Mai")
                    {
                        hplTag.NavigateUrl = "/Tu-Khoa-Khuyen-Mai/" + dataItem["TagItem"].ToString() + "/Tim-Kiem.htm";
                    }
                    else if(Request.QueryString["Page"].ToString() == "Chi-Tiet-Tin-Tuc")
                    {
                        hplTag.NavigateUrl = "/Tu-Khoa-Tim-Tuc/" + dataItem["TagItem"].ToString() + "/Tim-Kiem.htm";
                    }
                    hplTag.Text = dataItem["TagItem"].ToString() + ", ";
                    hplTag.ToolTip = dataItem["TagItem"].ToString();
                }
            }
        }
    }
}